package com.mobilesales.android.infrastructure;

import com.mobilesales.android.R;
import com.mobilesales.android.util.MessageBox;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

public class SyncItemTask extends AsyncTask<Void, Integer, Boolean> implements
		IProgressUpdater {

	Context context = null;
	int SyncNumber = -1;
	ProgressDialog dialog;

	/**
	 * 
	 * @param context
	 * @param SyncNum
	 *            final int Client = 1; final int Task = 2; final int TaskCyclic
	 *            = 3; final int Order = 4; final int Message = 5;
	 */
	public SyncItemTask(Context context, int SyncNum) {
		this.context = context;
		this.SyncNumber = SyncNum;
	}

	@Override
	protected void onPreExecute() {
		dialog = new ProgressDialog(context);
		dialog.setMessage(context.getString(R.string.SyncLoading));
		dialog.setCancelable(false);
		dialog.show();
	}

	public void onProgressUpdated(int progress) {
		// TODO Auto-generated method stub

	}

	@Override
	protected Boolean doInBackground(Void... params) {
		// TODO Auto-generated method stub
		SyncService sync = new SyncService(context, this);
		return sync.synchronize(SyncNumber);
	}

	@Override
	protected void onPostExecute(Boolean success) {
		dialog.dismiss();
		if (success)
			MessageBox
					.show(context, context.getString(R.string.SyncSuccessful));
		else
			MessageBox.show(context, context.getString(R.string.ErrorSync));
	}
}
